package com.example.ordermealservice.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commonutils.R;
import com.example.ordermealservice.entity.OrderClassification;
import com.example.ordermealservice.service.OrderClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/ordermealservice/orderclassification")
public class OrderClassificationController {
    @Autowired
    private OrderClassificationService orderClassificationService;
    // 新建菜品分类
    // 如果只有一级分类，那么一级已存在就不能新增
    // 如果已经有家常菜-鲁菜，那么再新增一个家常菜就不合理
    @PostMapping("/createCategory")
    public R createCategory(@RequestBody OrderClassification orderClassification){
        String category = orderClassification.getCategory();
        String subCategory = orderClassification.getSubCategory();
        if (category == null){
            return R.error().message("一级分类不能为空");
        }
        // 如果只有一级分类，那么一级已存在就不能新增
        if (subCategory == null){
            LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderClassification::getCategory,category);
            int count = orderClassificationService.count(queryWrapper);
            if (count != 0){
                return R.error().message("该菜品分类已存在");
            }
        }else {
            // 如果有二级分类，那么两级同时相同则不能新增
            LambdaQueryWrapper<OrderClassification> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(OrderClassification::getSubCategory,subCategory);
            queryWrapper2.eq(OrderClassification::getCategory,category);
            if (orderClassificationService.count(queryWrapper2) != 0){
                return R.error().message("该菜品分类已存在");
            }
        }
        LambdaQueryWrapper<OrderClassification> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OrderClassification::getCategory,category);
        queryWrapper1.last("and sub_category is null");
        orderClassificationService.remove(queryWrapper1);
        orderClassificationService.save(orderClassification);
        return R.ok().message("新建菜品分类成功");
    }
    //查询菜品一级分类，将查出来的结果存到HashSet集合中进行去重
    @GetMapping("/getCategory")
    public R getCategory(){
        QueryWrapper<OrderClassification> queryWrapper = new QueryWrapper<>();
        QueryWrapper<OrderClassification> category = queryWrapper.select("category");
        List<OrderClassification> list = orderClassificationService.list(category);
        Set<OrderClassification> s = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            s.add(list.get(i));
        }
        return R.ok().data("category",s);
    }
    //查询菜品二级分类
    @GetMapping("/getSubCategory/{category}")
    public R getSubCategory(@PathVariable String category){
        LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderClassification::getCategory,category);
        List<OrderClassification> subCategory = orderClassificationService.list(queryWrapper);
        return R.ok().data("subCategory",subCategory);
    }
    //查询菜品的一级分类和二级分类，以树形结构展示
    @GetMapping("/getAllCategory")
    public R getAllCategory(){
        Map<String,List<String>> map = new HashMap<>();
        List<String> oneSubject = orderClassificationService.getOneSubject();
        //遍历每一个一级目录，将每一个一级目录下的二级目录存入Map集合
        for (int i = 0; i < oneSubject.size(); i++) {
            List<String> twoSubject = new ArrayList<>();
            twoSubject = orderClassificationService.getTwoSubject(oneSubject.get(i));
            map.put(oneSubject.get(i),twoSubject);
        }
        return R.ok().data("dishCategoryVo",map);
    }
    //TODO Excel表格批量导入菜品分类信息
    @PostMapping("/bulkAddCategory")
    public R bulkAddCategory(MultipartFile file){
        orderClassificationService.bulkAddCategory(file,orderClassificationService);
        return R.ok();
    }
    //删除一级分类
    @DeleteMapping("/delCategory/{category}")
    public R delCategory(@PathVariable String category){
        LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderClassification::getCategory,category);
        orderClassificationService.remove(queryWrapper);
        return R.ok();
    }
    //删除二级分类
    @DeleteMapping("/delSubCategory/{category}/{subCategory}")
    public R delSubCategory(@PathVariable String category, @PathVariable String subCategory){
        LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderClassification::getCategory,category);
        queryWrapper.eq(OrderClassification::getSubCategory,subCategory);
        orderClassificationService.remove(queryWrapper);
        return R.ok();
    }
}

